package com.fiberhome.ztelauncher.uilib;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.fiberhome.ztelauncher.util.DisplayUtil;


/**
 * Created by Deed on 2018/4/3.
 */

public class NavItem extends FrameLayout {
    private OnItemSelectedListener itemSelectedListener;
    private ImageView icon;
    private TextView text;

    public NavItem(@NonNull Context context) {
        super(context);
        init(context);
    }

    public NavItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NavItem(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        icon = new ImageView(context);
        LayoutParams iconLp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        iconLp.gravity = Gravity.CENTER;
        icon.setLayoutParams(iconLp);
        addView(icon);
        text = new TextView(context);
        LayoutParams textLp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        textLp.gravity = Gravity.CENTER;
        int margin = DisplayUtil.px2dip(context,5);
        textLp.leftMargin = margin;
        textLp.topMargin = margin;
        textLp.rightMargin = margin;
        textLp.bottomMargin = margin;
        text.setLayoutParams(textLp);
        text.setGravity(Gravity.CENTER);
        addView(text);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (itemSelectedListener != null) {
            itemSelectedListener.onSelected(this,selected);
        }
    }

    /**
     * return value.
     */
    public ImageView getIcon() {
        return icon;
    }

    /**
     * return value.
     */
    public TextView getText() {
        return text;
    }

    /**
     * set value
     *
     * @param itemSelectedListener value
     */
    public void setItemSelectedListener(OnItemSelectedListener itemSelectedListener) {
        this.itemSelectedListener = itemSelectedListener;
    }

    public interface OnItemSelectedListener {
        void onSelected(NavItem item,boolean selected);
    }
}
